package com.xiaobange.controller.system;

import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.xiaobange.dto.UserPage;
import com.xiaobange.service.UserService;
import com.xiaobange.vo.ResultResponse;
import com.xiaobange.entity.User;
import jakarta.validation.Valid;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author 小班哥
 * @Date 2025/5/6 13:23
 * @Explain 系统用户操作控制器
 */
@RestController
@RequestMapping("/sys/user")
public class SystemUserController {

    @Autowired
    private UserService userService;

    /**
     * 获取系统用户分页数据
     *
     * @param userPage 分页参数
     * @return 结果
     */
    @SaCheckPermission(value = "system:user:view",orRole = "admin")
    @PostMapping("/page")
    public ResultResponse<List<User>> sysUserPage(@Valid @RequestBody UserPage userPage) {
        return userService.selectPage(userPage);
    }

    /**
     * 查询指定用户数据
     * @param userid 用户ID
     * @return 结果
     */
    @SaCheckPermission(value = "system:user:view",orRole = "admin")
    @GetMapping("/query/{userid}")
    public User query(@PathVariable String userid) {
        return userService.queryId(userid);
    }


    /**
     * 修改用户数据
     * @param user
     * @return
     */
    @SaCheckPermission(value = "system:user:update",orRole = "admin")
/*    @SaCheckOr(
            permission = @SaCheckPermission(value = {"system:user:update"}),
            role = @SaCheckRole(value = "admin")

    )*/
    @PutMapping("/update")
    public ResultResponse<String> updateInfo(@RequestBody User user) {
        return userService.updateInfo(user);
    }


    /**
     * 修改用户密码
     *
     * @param userId   用户ID
     * @param password 新密码
     * @return 结果
     */
    @SaCheckPermission(value = "system:user:update",orRole = "admin")
    @PutMapping("/updatePwd")
    public ResultResponse<?> updatePassword(@PathParam("userId") String userId,
                                            @PathParam("password") String password) {
        return userService.updatePassword(userId,password);
    }

    /**
     * 删除系统用户
     * @param userId
     * @return
     */
    @SaCheckPermission(value = "system:user:delete",orRole = "admin")
    @DeleteMapping("/del/{userId}")
    public ResultResponse<Integer> delUser(@PathVariable String userId){
        return userService.delById(userId);
    }


}
